package edu.nf.takeoutdemo.web.admin;

import edu.nf.takeoutdemo.commons.base.BaseController;
import edu.nf.takeoutdemo.commons.result.PageResult;
import edu.nf.takeoutdemo.commons.result.Result;
import edu.nf.takeoutdemo.model.dto.CategoryDTO;
import edu.nf.takeoutdemo.model.dto.CategoryPageQueryDTO;
import edu.nf.takeoutdemo.model.entity.Category;
import edu.nf.takeoutdemo.service.CategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author zjl
 * @date 2025/10/25
 */
@RestController("adminCategoryController")
@RequiredArgsConstructor
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController extends BaseController {
    private final CategoryService categoryService;
    @PostMapping
    public Result<?> save(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类：{}", categoryDTO);
        categoryService.save(categoryDTO);
        return success();
    }

    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分页查询：{}", categoryPageQueryDTO);
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return success(pageResult);
    }

    @DeleteMapping
    public Result<?> deleteById(Long id){
        log.info("删除分类：{}", id);
        categoryService.deleteById(id);
        return success();
    }

    @PutMapping
    public Result<String> update(@RequestBody CategoryDTO categoryDTO){
        categoryService.update(categoryDTO);
        return success();
    }

    @PostMapping("/status/{status}")
    public Result<String> startOrStop(@PathVariable("status") Integer status, Long id){
        categoryService.startOrStop(status,id);
        return success();
    }

    @GetMapping("/list")
    public Result<List<Category>> list(Integer type){
        List<Category> list = categoryService.list(type);
        return success(list);
    }
}
